<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>编辑文章</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <!-- jq -->
    <script src="./libs/jquery/jquery.min.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            编辑文章
        </div>
        <div class="container-fluid common_con">
            <form class="form-horizontal article_form" id="form">
                <div class="form-group">
                    <label for="inputTitle" class="col-sm-2 control-label">文章标题：</label>
                    <div class="col-sm-10">
                        <input type="text" name="title" class="form-control title" id="inputTitle" value="文章标题文字">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCover" class="col-sm-2 control-label">文章封面：</label>
                    <div class="col-sm-10">
                        <img src="./images/2.jpg" class="article_cover">
                        <input name="cover" type="file" id="inputCover" onchange="changeCoverImage()">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputCategory" class="col-sm-2 control-label">文章类别：</label>
                    <div class="col-sm-4">
                        <select id="category" class="form-control category" name="categoryId">
                            <option value="">所有分类</option>
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-2 control-label">发布时间：</label>
                    <div class="col-sm-4">
                        <input id="articleDate" name="date" class="jeinput" type="text" placeholder="请选择发布日期" readonly>
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">文章内容：</label>
                    <div class="col-sm-10">
                        <!-- 富文本编辑区域 -->
                        <textarea id="articleContent">请输入文章内容</textarea>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-success btn-edit"
                            onclick="return saveArticle('已发布')">修改</button>
                        <button type="submit" class="btn btn-default btn-draft"
                            onclick="return saveArticle('')">存为草稿</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <script src="./js/http.js"></script>
    <script src="./libs/art-template/template-web.js"></script>
    <script src="./libs/layui/layui.js"></script>
    <script src="./libs/tinymce_5.5.1/tinymce.min.js"></script>

    <!-- 所有类别模板 -->
    <script type="text/html" id="listClass">
        {{each data}}
        <option value="{{$value.id}}">{{$value.name}}</option>
        {{/each}}
    </script>
    <script>
        $(function () {
            //所有类别
            listClass()
            //发布时间
            codeTime()
            //富文本框
            textareaCode()
            //拿旧数据
            getCode()
        })
        //类别
        function listClass() {
            myHeader('/admin/category/list', 'get', null, function (resData) {
                $('#category').append(template('listClass', resData))
            })
        }
        //发布时间
        function codeTime() {
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                //执行一个laydate实例
                laydate.render({
                    elem: '#articleDate' //指定元素
                });
            });
        }
        //富文本框
        function textareaCode() {
            tinymce.init({
                selector: '#articleContent',
                language: 'zh_CN'
            });
        }
        //拿id
        function getId() {
            let hrefUrl = window.location.href.split('?');
            let hrefId = hrefUrl[1].split('=');
            return hrefId[1];
        }
        //通过id拿旧数据,再把旧数据放到文件中
        function getCode() {
            myHeader('/admin/article/search', 'get', { id: getId() }, function (resData) {
                if (resData.code != 200) {
                    alert(resData.msg);
                    return false;
                }
                $('#inputTitle').val(resData.data.title);
                $('.article_cover').prop('src', resData.data.cover)
                $('#category').val(resData.data.categoryId);
                $('#articleDate').val(resData.data.date);
                //富文本框
                $('#articleContent').val(resData.data.content)
            })
        }

        function changeCoverImage() {
            let file = document.querySelector('#inputCover').files[0];
            let imgUrl = URL.createObjectURL(file);
            $('.article_cover').prop('src', imgUrl)
        }

        //点击修改编辑功能
        function saveArticle(state_text) {
            // 获取
            window.event.preventDefault();
            let id = getId();
            let title = $('#inputTitle').val();
            let cover = document.querySelector('#inputCover').files[0];
            let categoryId = $('#category').val();
            let date = $('#articleDate').val();
            let content = tinyMCE.editors['articleContent'].getContent();
            let state = state_text;
            //  设置对象
            let formData = new FormData();
            formData.append('id', id);
            formData.append('title', title);
            formData.append('cover', cover);
            formData.append('categoryId', categoryId);
            formData.append('date', date);
            formData.append('content', content);
            formData.append('state', state);
            // 调用ajax
            myHeader('/admin/article/edit', 'post', formData, function (resData) {
                location.href = "./article_list.html";
            })

        }
    </script>
</body>

</html>